/**
 * Created by John on 2015/9/23.
 */


function _initOnePageRemote() {
    $("#onePageRemote").jqGrid({
        url: 'http://trirand.com/blog/phpjqgrid/examples/jsonp/getjsonp.php?callback=?&amp;qwery=longorders',
        height: 250,
        autowidth: true,
        mtype: "GET",
        styleUI: 'Bootstrap',
        datatype: "jsonp",
        colModel: [
            {label: 'OrderID', name: 'OrderID', key: true, width: 75,editable:true},
            {label: 'Customer ID', name: 'CustomerID', width: 150,editable:true},
            {label: 'Order Date', name: 'OrderDate', width: 150,editable:true},
            {label: 'Freight', name: 'Freight', width: 150,editable:true},
            {label: 'Ship Name', name: 'ShipName', width: 150,editable:true}
        ],
        viewrecords: true,
        rowNum: 20,
        pager: "#onePageRemotePager"
    })
        .navGrid('#onePageRemotePager',{closeOnEscape:true,edit:true,add:true,del:true,search:false},{closeAfterEdit:true});
}


function _initStackoverflow() {
    $("#stackoverflow").jqGrid({
        caption: "Load live data from stackoverflow",
        autowidth: true,
        //width: 780,
        height: 200,
        datatype: 'local',
        colModel: [{
            label: 'Title',
            name: 'Title',
            width: 150,
            formatter: formatTitle
        }, {
            label: 'Link',
            name: 'Link',
            width: 80,
            formatter: formatLink
        }, {
            label: 'View Count',
            name: 'ViewCount',
            width: 35,
            sorttype: 'integer',
            formatter: 'number',
            align: 'right'
        }, {
            label: 'Answer Count',
            name: 'AnswerCount',
            width: 25
        }],
        pager: "#stackoverflowPager",
        viewrecords: true, // show the current page, data rang and total records on the toolbar
        rowNum: 15
    });

    _fetchStackoverflowGridData();
}

function formatTitle(cellValue, options, rowObject) {
    return cellValue.substring(0, 50) + "...";
}

function formatLink(cellValue, options, rowObject) {
    return "<a href='" + cellValue + "'>" + cellValue.substring(0, 25) + "...</a>";
}

function _fetchStackoverflowGridData() {
    var gridArrayData = [];
    var $grid = $("#stackoverflow");
    // show loading message
    $grid[0].grid.beginReq();
    var dataUrl = "http://api.stackexchange.com/2.2/questions?order=desc&sort=activity&tagged=jqgrid&site=stackoverflow";
    $.getJSON(dataUrl, function (result) {
        for (var i = 0; i < result.items.length; i++) {
            var item = result.items[i];
            gridArrayData.push({
                Title: item.title,
                Link: item.link,
                CreationDate: item.creation_date,
                ViewCount: item.view_count,
                AnswerCount: item.answer_count
            });
        }
        // set the new data
        $grid.jqGrid('setGridParam', {data: gridArrayData});
        // hide the show message
        $grid[0].grid.endReq();
        // refresh the grid
        $grid.trigger('reloadGrid');
    })

}